package com.jiinfo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.core.ActionKey;
import com.jfinal.plugin.activerecord.Record;
import com.jiinfo.base.BaseController;
import com.jiinfo.interceptor.AuthInterceptor;
import com.jiinfo.model.Resource;
import com.jiinfo.vo.BaseVo;

public class ResourceController extends BaseController{
	
	private static final Logger logger = LoggerFactory.getLogger(AuthInterceptor.class);
	
	@ActionKey("/manage/resource/")
	public void manage_resource(){
		
		String childName="child";
		List<Record> resourceTree=Resource.dao.getTreeResources(childName);
		List<Record> list=new ArrayList<>();
		for (Record record : resourceTree) {
			getListForTree(childName,record,list);
		}
		
		setAttr("resourceList", list);
		setAttr("pageTitle", getPageTitle("资源管理", "",
				new String[]{"<i class='icon-home'></i>首页","资源管理"},
				new String[]{"/manage","/manage/resource/"}));
		renderJsp("/manage/resource/resource-list.jsp");
		
	}
	
	
	@ActionKey("/manage/resource/add/")
	public void manage_add(){
		Record user=getCurrUser();
		
		Resource vo=getModel(Resource.class,"vo");
		Resource.dao.add(vo,user);
		
		redirect("/manage/resource/");
		
	}
	
	@ActionKey("/manage/resource/edit/")
	public void manage_edit(){
		Record user=getCurrUser();
		
		Resource vo=getModel(Resource.class,"vo");
		Resource.dao.edit(vo,user);
		
		redirect("/manage/resource/");
		
	}
	
	@ActionKey("/manage/resource/delete/")
	public void manage_delete(){
		
		String id=getPara("id");
		Resource.dao.deleteNodeAndSubNode(id);
		redirect("/manage/resource/");
		
	}
	
	@ActionKey("/manage/resource/viewJson/")
	public void manage_viewJson(){
		
		String id=getPara("id");
		
		Map<String,Object> map=new HashMap<>();
		Resource resource=Resource.dao.findById(id);
		Resource resourceParent=Resource.dao.findById(resource.getStr("parentId"));
		map.put("resource", resource);
		map.put("resourceParent", resourceParent);
		
		BaseVo baseVo=new BaseVo();
		baseVo.setFlag(true);
		baseVo.setData(map);
		
		renderJson(baseVo);
		
	}
}
